package org.example.mq.mqserver.core;

import java.io.Serializable;
import java.util.Arrays;
import java.util.UUID;

/*
* 表示传递的消息
* */
public class Message implements Serializable {
//    版本
    private static final long serialVersionUID = 1L;
//    信息头
    //创建实例，避免范围null指针
    private BasicProperties basicProperties = new BasicProperties();

//    内容部分
    private byte[] body;

//    辅助属性
    /*
    * 起始位置和结束位置：【offsetBeg，offsetEnd)
    * */
    private long offsetBeg = 0;
    private long offsetEnd = 0;
    //0x1表示有效，0x0表示删除a
    private byte isValid = 0x1;


/*
* 提供创建对象方法（使用工厂方法）
* */

    public static Message createMessage(String routingKey,byte[] body ,BasicProperties basicProperties){
        Message message = new Message();
        if(basicProperties!=null){
            message.setBasicProperties(basicProperties);
        }
        message.setRoutingKey(routingKey);
        message.setMessageId("M-"+ UUID.randomUUID());
        message.body = body;
        return message;

    }

/*
* 序列化和反序列化
* */


    //将头部进行封装

    public String getMessageId(){
        return basicProperties.getMessageId();
    }
    public void setMessageId(String messageId){
        basicProperties.setMessageId(messageId);
    }

    public String  getRoutingKey(){
        return basicProperties.getRoutingKey();
    }

    public void setRoutingKey(String routingKey){
        basicProperties.setRoutingKey(routingKey);
    }

    public BasicProperties getBasicProperties() {
        return basicProperties;
    }

    public void setBasicProperties(BasicProperties basicProperties) {
        this.basicProperties = basicProperties;
    }

    public byte[] getBody() {
        return body;
    }

    public void setBody(byte[] body) {
        this.body = body;
    }

    public long getOffsetBeg() {
        return offsetBeg;
    }

    public void setOffsetBeg(long offsetBeg) {
        this.offsetBeg = offsetBeg;
    }

    public long getOffsetEnd() {
        return offsetEnd;
    }

    public void setOffsetEnd(long offsetEnd) {
        this.offsetEnd = offsetEnd;
    }

    public byte getIsValid() {
        return isValid;
    }

    public void setIsValid(byte isValid) {
        this.isValid = isValid;
    }

    @Override
    public String toString() {
        return "Message{" +
                "basicProperties=" + basicProperties +
                ", body=" + Arrays.toString(body) +
                ", offsetBeg=" + offsetBeg +
                ", offsetEnd=" + offsetEnd +
                ", isValid=" + isValid +
                '}';
    }
}
